#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn][maxn];
LL dp1[maxn][maxn][2],dp2[maxn][maxn][2];
LL get(LL x){
   LL temp=x;LL sum=0;
   while(x>0){
        if(x&1) sum++;
        x>>=1;
   }
   return temp+sum;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++){
    for(LL j=1;j<=m;j++){
        cin>>a[i][j];
    }
  }
  LL ans=0;
  for(LL i=1;i<=n;i++){
    for(LL j=2;j<=m;j++){
        dp1[i][j][0]=max(dp1[i][j-1][1]+get(a[i][j-1]^a[i][j]),dp1[i][j-1][0]);
        dp1[i][j][1]=max(dp1[i][j-1][1],dp1[i][j-1][0]);
    }
    ans+=max(dp1[i][m][0],dp1[i][m][1]);
  }
  for(LL j=1;j<=m;j++){
    for(LL i=2;i<=n;i++){
        dp2[i][j][0]=max(dp2[i-1][j][1]+get(a[i-1][j]^a[i][j]),dp2[i-1][j][0]);
        dp2[i][j][1]=max(dp2[i-1][j][1],dp2[i-1][j][0]);
    }
    ans+=max(dp2[n][j][0],dp2[n][j][1]);
  }
  cout<<ans<<"\n";
return 0;
}
